1454F - Array Partition - CodeForces Solution


binary search data structures greedy two pointers *2100

Please click on ads to support us..

C++ Code:

// https://codeforces.com/problemset/problem/1454/F
#include <bits/stdc++.h>
using namespace std;
const int N = 2 * 1e5 + 5;
const int LOG = 18;
int mn[N][LOG], mx[N][LOG];
bool fl;
int getmin(int l, int r)
{
    int len = r - l + 1;
    int k = 0;
    while ((1 << (k + 1)) <= len)
        ++k;
    return min(mn[l][k], mn[r - (1 << k) + 1][k]);
}
int getmax(int l, int r)
{
    int len = r - l + 1;
    int k = 0;
    while ((1 << (k + 1)) <= len)
        ++k;
    return max(mx[l][k], mx[r - (1 << k) + 1][k]);
}
bool valid(int l, int r, int n)
{
    if (l < 1 || l >= r - 1 || r > n)
        return 0;
    int lf = getmax(1, l);
    int md = getmin(l + 1, r - 1);
    int rg = getmax(r, n);
    if (lf == md && lf == rg)
    {
        cout << "YES\n"
             << l << ' ' << r - l - 1 << ' ' << n - r + 1 << '\n';
        fl = 1;
        return 1;
    }
    return 0;
}
int main()
{
    cin.tie(0)->ios_base::sync_with_stdio(0);
    int t;
    cin >> t;
    while (t--)
    {
        fl = 0;
        int n;
        cin >> n;
        vector<int> a(n + 5);
        for (int i = 1; i <= n; ++i)
            cin >> a[i], mn[i][0] = mx[i][0] = a[i];
        for (int j = 1; j < LOG; ++j)
            for (int i = 1; i + (1 << j) - 1 <= n; ++i)
            {
                mn[i][j] = min(mn[i][j - 1], mn[i + (1 << (j - 1))][j - 1]);
                mx[i][j] = max(mx[i][j - 1], mx[i + (1 << (j - 1))][j - 1]);
            }
        int lf = 0;
        int r = n;
        for (int l = 1; l < n - 1; ++l)
        {
            lf = max(lf, a[l]);
            while (r - 1 > l && a[r] <= lf)
                --r;
            ++r;
            while (r - 1 <= l)
                ++r;
            if (valid(l, r, n) || valid(l - 1, r, n) || valid(l, r + 1, n))
                break;
        }
        if (!fl)
            cout << "NO\n";
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

979A - Pizza Pizza Pizza
731A - Night at the Museum
742A - Arpa’s hard exam and Mehrdad’s naive cheat
1492A - Three swimmers
1360E - Polygon
1517D - Explorer Space
1230B - Ania and Minimizing
1201A - Important Exam
676A - Nicholas and Permutation
431A - Black Square
474B - Worms
987B - High School Become Human
1223A - CME
1658B - Marin and Anti-coprime Permutation
14B - Young Photographer
143A - Help Vasilisa the Wise 2
320A - Magic Numbers
1658A - Marin and Photoshoot
514A - Chewbaсca and Number
382A - Ksenia and Pan Scales
734B - Anton and Digits
1080A - Petya and Origami
1642D - Repetitions Decoding
1440A - Buy the String
1658F - Juju and Binary String
478A - Initial Bet
981A - Antipalindrome
365A - Good Number
1204B - Mislove Has Lost an Array
1409D - Decrease the Sum of Digits